// pages/user-my-order-details/user-my-order-details.js
var app = getApp();
Page({
 
  /**总价格
   * 页面的初始数据 
   */
  data: {
    orderid:"",//订单ID
    orderType: "",//订单类型2医美1术前术后
    operation: "",//术前后详情数据//医美的详情数据
    imgUrl: app.globalData.imgUrl,//拼接图片路径
    localImg: app.globalData.staticimgUrl,  // 本地图片
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoading();
    this.setData({
      orderid: options.id,
      orderType: options.orderType
    });
    this.getOrderDetail();
  },

  getOrderDetail:function(){
    let orderType = this.data.orderType;
    //获取订单详情判断
    if (orderType == 1) {
      this.operation();// 获取用户订单详情  术前术后的
    } else {
      this.orderdetails();// 获取用户订单详情 医美的
    }
  },

  // 获取用户订单详情医美的
  orderdetails:function(){
    var that = this;
    // 获取用户订单详情
    app.request({
      url: 'shopp/yimeiGoodsOrderApi/getOrderDetail',
      data: {
        orderid: that.data.orderid,//订单ID
      },
      success(res) {
        wx.hideLoading();
        that.setData({
          operation:res.data
        })
      }
    }) 
  } ,

  // 获取用户订单详情医术前术后的
  operation: function () {
    var that = this;
    // 获取用户订单详情
    app.request({
      url:'shopp/goodsOrderApi/getOrderDetail/' + that.data.orderid,
      method:"GET",
      success(res){
        wx.hideLoading();
        var operation = res.data;
        var znum=0;
        for (var i = 0; i < operation.goodsArray.length;i++){
          znum = znum + operation.goodsArray[i].number;
          // console.log('znum', znum);
        }
        operation.znum=znum;
        // console.log(operation)
        that.setData({
          operation
        })
      }
    })
  },


  //取消订单按钮 1是术前术后  2是医美
  qxorder: function (e) {
    var that = this;
    let sid = e.currentTarget.dataset.sid;
    let { orderType } = this.data;
    if (orderType == 2) {
      //删除医美订单
      app.request({
        url: 'shopp/yimeiGoodsOrderApi/cancelOrder',
        data: {
          id: sid,
        },
        success(res) {
          wx.showToast({
            title: "成功",
            icon: 'success',
            duration: 1000,
            success: function(){
              that.getOrderDetail();
            }
          });
        }
      })
    } else if (orderType == 1) {
      //删除术前术后订单
      app.request({
        url: 'shopp/goodsOrderApi/cancelOrder',
        data: {
          id: sid,
        },
        success(res) {
          wx.showToast({
            title: "成功",
            icon: 'success',
            duration: 1000,
            success: function () {
              that.getOrderDetail();
            }
          });
        }
      })
    }
  },
  //没发货查看物流提示
  log: function () {
    wx.showModal({
      title: '提示',
      content: '正在安排，请耐心等待，我们会尽快给您发货',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //查看物流
  logistics: function (e) {
    var that = this;
    app.request({
      url: 'shopp/goodsOrderApi/orderLogistics',
      data: {
        id: e.currentTarget.dataset.orderid
      },
      success(res) {
        console.log("查看物流结果", res)
      }
    })
  },
  //确认收货
  receipt: function (e) {
    var that = this;
    app.request({
      url: 'shopp/goodsOrderApi/confirmCollectGoods',
      data: {
        id: e.currentTarget.dataset.orderid
      },
      success(res) {
        wx.switchTab({
          url: '/pages/doctor-my/doctor-my',
        })
        wx.showToast({
          title: '收货成功',
          icon: 'success',
          duration: 1000,
        })
      }
    })
  },

  //发起支付请求
  fkorder: function (e) {
    const { operation } = this.data;
    if (operation.ifPayment) {
      wx.navigateTo({
        url: `../user-shop-goods-order-pay/user-shop-goods-order-pay?res=${operation.orderId}&totalMoney=${operation.paymentMoney}&payType=1&orderType=1`,
      });
    } else {
      wx.navigateTo({
        url: `../user-shop-goods-balance-pay/user-shop-goods-balance-pay?res=${operation.orderId}&balance=${operation.useBalance}`,
      });
    }
  }
})